package TUILayer;
import java.util.Scanner;
import ControlLayer.*;


/**
 * Write a description of class LoanUI here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class LoanUI
{
    // instance variables - replace the example below with your own
    private LoanCtr loanCtr;
    private DVDCtr dvdCtr;
    private AddressCtr adrCtr;

    /**
     * Constructor for objects of class LoanUI
     */
    public LoanUI(DVDCtr dvdCtr, AddressCtr adrCtr)
    {
        this.dvdCtr = dvdCtr;
        this.adrCtr = adrCtr;
        loanCtr = new LoanCtr();
        
    }
   
    
    public void start()
    {
        loanMenu();
    }
    
    public void loanMenu()
    {
        boolean exit = false;
        while(!exit)
        {
            int choice = writeLoanMenu();
            if(choice == 1)
            {
                // Make new loan
                createLoan();
            }
            else
            {
                if(choice == 2)
                {
                    //Show loan information
                    showLoanInformation();
                }
                else
                {
                    if(choice == 3)
                    {
                        //List all loans
                        Scanner keyboard = new Scanner(System.in);
                        listAllLoans();
                        System.out.println("\nType 'Next' to continue");
                        String next = keyboard.next();
                    }
                    else
                    {
                        if(choice == 4)
                        {
                            //Update loan
                            updateLoan();
                        }
                        else
                        {
                            if(choice == 5)
                            {
                                //Return copy
                                returnCopy();
                                System.out.println("Please type 'Next' to continue");
                                Scanner keyboard = new Scanner(System.in);
                                String next = keyboard.next();
                            }
                            else
                            {
                                if(choice == 6)
                                {
                                    //Delete loan
                                    deleteLoan();
                                }
                                else
                                {
                                    exit = true;
                                }//end else
                            }//end else
                        }//end else
                    }//end else
                }//end else
            }//end else
        }//end while
                    
                       
                                
                                
    }
    
    public int writeLoanMenu()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\f *** Loan Menu ***");
        System.out.println(" (1) Make new loan");
        System.out.println(" (2) Show information about one loan");
        System.out.println(" (3) List all loans");
        System.out.println(" (4) Update loan");
        System.out.println(" (5) Return copy");
        System.out.println(" (6) Delete loan");
        System.out.println(" (7) Return");
        System.out.print("\n Make your choice: ");
        int choice = keyboard.nextInt();
        return choice;
    }
    
    public void createLoan()
    {
        Scanner keyboard = new Scanner(System.in);
       System.out.println("\f *** Make new loan ***");
       System.out.println("\n** DVD List **");
       if(dvdCtr!=null)
       {
           dvdCtr.listAllDvds();
        }
       System.out.println("\n** Person List **");
       if(adrCtr!=null)
       {
           adrCtr.listAllPersons();
        }
       System.out.println("\nPlease enter the details for the new loan");
       long loanId = inputLoanId();
       long dvdId = inputDvdId();
       long personId = inputPersonId();
       System.out.print("(5) Borrow Date(dd/MM/yyyy): ");
       String borrowDate = keyboard.next();
       System.out.print("(6) Period (Days): ");
       String period = keyboard.next();
       if(loanCtr.createLoan(loanId,dvdId,personId,borrowDate,period))
       {
       System.out.println("Loan was created succesfully. Type 'Next' to continue");
       }
       else System.out.println("Loan was not created. Press 'Next' to continue");
       String next = keyboard.next();
    }
    
    private long inputLoanId()
    {
        boolean ok = false;
        long id = 0;
        while(!ok)
        {
        // makes an object keyboard to have input from the console
           Scanner keyboard = new Scanner(System.in);
           System.out.print("(1) ID:  ");
           try{
               
             id = keyboard.nextLong();
             ok = true;
           }
           catch (Exception e)
            {
             System.out.println("Please write a number");
             String input = keyboard.next();
           }
        }//end while
        return id;
    }
    
    private long inputDvdId()
    {
        boolean ok = false;
        long id = 0;
        while(!ok)
        {
        // makes an object keyboard to have input from the console
           Scanner keyboard = new Scanner(System.in);
           System.out.print("(2) DVD ID:  ");
           try{
               
             id = keyboard.nextLong();
             ok = true;
           }
           catch (Exception e)
            {
             System.out.println("Please write a number");
             String input = keyboard.next();
           }
        }//end while
        return id;
    }
    
    
    
    private long inputPersonId()
    {
        boolean ok = false;
        long id = 0;
        while(!ok)
        {
        // makes an object keyboard to have input from the console
           Scanner keyboard = new Scanner(System.in);
           System.out.print("(3) Person ID:  ");
           try{
               
             id = keyboard.nextLong();
             ok = true;
           }
           catch (Exception e)
            {
             System.out.println("Please write a number");
             String input = keyboard.next();
           }
        }//end while
        return id;
    }
    public void showLoanInformation()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\f *** Show loan information ***");
        System.out.print("\n");
        loanCtr.listAllLoans();
        System.out.print("Please enter the ID of the loan:");
        long id = keyboard.nextLong();
        loanCtr.printLoan(id);
    }
    
    public void listAllLoans()
    {
        loanCtr.listAllLoans();
    }
    
    public int updateLoanMenu()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Please enter the number of the detail you want to update");
        System.out.println("(1) Period");
        System.out.println("(2) Borrow Date");
        System.out.println("(3) State");
        int choice = keyboard.nextInt();
        return choice;
    }
    public void updateLoan()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\f*** Update loan ***");
        listAllLoans();
        System.out.println("\nPlease enter the ID of the loan you wish to update:");
        long id = keyboard.nextLong();
        int choice = updateLoanMenu();
        if (choice==1)
        {
            System.out.println("Please enter the new Period (in days)");
            String period = keyboard.next();
            if(loanCtr.updateLoanPeriod(id,period))
            System.out.println("Loan Period updated succesfully");
            System.out.println("Please type 'Next' to continue");
            String next = keyboard.next();
        }
        if (choice==2)
        {
            System.out.println("Please enter the new Borrow Date (dd/MM/yyyy)");
            String borrowDate = keyboard.next();
            if(loanCtr.updateLoanBorrowDate(id,borrowDate))
            System.out.println("Loan Borrow Date updated succesfully");
            System.out.println("Please type 'Next' to continue");
            String next = keyboard.next();
        }
        if (choice==3)
        {
            System.out.println("Please enter the new State");
            String state = keyboard.next();
            if(loanCtr.updateLoanState(id,state))
            System.out.println("Loan State updated succesfully");
            System.out.println("Please type 'Next' to continue");
            String next = keyboard.next();
        }
        
    }
    
    public void returnCopy()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\f*** Return Copy ***");
        listAllLoans();
        System.out.print("\nPlease enter the ID of the loan: ");
        long id = keyboard.nextLong();
        loanCtr.returnCopy(id);
        
    }
    
    public void deleteLoan()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\f*** Delete Loan ***");
        listAllLoans();
        System.out.print("\nPlease enter the ID of the loan: ");
        long id = keyboard.nextLong();
        if(loanCtr.removeLoan(id))
        {
            System.out.println("Loan deleted succesfully");
        }
    }

   
}
